package view;

import control.Calculos;
import javax.swing.JOptionPane;

public class TrianguloForm extends javax.swing.JInternalFrame {
    //Variavel usasa na singleton
    private static TrianguloForm instance;
    /** Creates new form TrianguloForm */
    public TrianguloForm() {
        initComponents();
        resultadoLabel.setVisible(false);        
    }
    
    //Metodo usado no singleton
    public static TrianguloForm getInstance(){
        if(instance == null){
            instance = new TrianguloForm();
        }
        return instance;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Label1 = new javax.swing.JLabel();
        baseTextField = new javax.swing.JTextField();
        calcularButton = new javax.swing.JButton();
        limparButton = new javax.swing.JButton();
        resultadoLabel = new javax.swing.JLabel();
        Label2 = new javax.swing.JLabel();
        alturaTextField = new javax.swing.JTextField();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Área do triângulo");

        Label1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        Label1.setText("Base:");

        baseTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        calcularButton.setText("Calcular");
        calcularButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                calcularButtonActionPerformed(evt);
            }
        });

        limparButton.setText("Limpar");
        limparButton.setMaximumSize(new java.awt.Dimension(71, 23));
        limparButton.setMinimumSize(new java.awt.Dimension(71, 23));
        limparButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                limparButtonActionPerformed(evt);
            }
        });

        resultadoLabel.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        resultadoLabel.setForeground(new java.awt.Color(51, 51, 255));
        resultadoLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        resultadoLabel.setText("A área do triângulo é: ");

        Label2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        Label2.setText("Altura:");

        alturaTextField.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(36, 36, 36)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(limparButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(Label1)
                        .addGap(18, 18, 18)
                        .addComponent(baseTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(calcularButton, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(Label2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(alturaTextField)))
                .addContainerGap(35, Short.MAX_VALUE))
            .addComponent(resultadoLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 252, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Label1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(baseTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Label2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(alturaTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13)
                .addComponent(calcularButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(limparButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(resultadoLabel)
                .addContainerGap(29, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void calcularButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_calcularButtonActionPerformed
    if (!baseTextField.getText().equals("") && !alturaTextField.getText().equals("")) {
        Calculos calculo = new Calculos();
        resultadoLabel.setText("");
        try {
            resultadoLabel.setText(calculo.calcularAreaTriangulo(Double.parseDouble(baseTextField.getText()),Double.parseDouble(alturaTextField.getText())));
        } catch (Exception e) {
            System.out.println(e.getClass().getCanonicalName() + ": " + e.getMessage());//para uso interno em testes
            JOptionPane.showMessageDialog(this, "Entrada Inválida");
            baseTextField.grabFocus();
        }
        resultadoLabel.setVisible(true);
    }else{
        JOptionPane.showMessageDialog(this, "Todos campos são obrigátorios para efetuar o cálculo");
        baseTextField.grabFocus();
    }
}//GEN-LAST:event_calcularButtonActionPerformed

private void limparButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_limparButtonActionPerformed
    baseTextField.setText("");
    alturaTextField.setText("");
    resultadoLabel.setVisible(false);
    baseTextField.grabFocus();
}//GEN-LAST:event_limparButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel Label1;
    private javax.swing.JLabel Label2;
    private javax.swing.JTextField alturaTextField;
    private javax.swing.JTextField baseTextField;
    private javax.swing.JButton calcularButton;
    private javax.swing.JButton limparButton;
    private javax.swing.JLabel resultadoLabel;
    // End of variables declaration//GEN-END:variables
}
